package com.binguo.api.service;


import com.binguo.common.paymodel.YunPengWXPayConfig;

import com.github.wxpay.sdk.WXPay;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class YunPengWXPayService {

    private static Logger logger = LoggerFactory.getLogger(YunPengWXPayService.class);

    public static String success = "SUCCESS";

    private WXPay wxPay = new WXPay(new YunPengWXPayConfig());
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");


    public static void main(String[] args) {
        new YunPengWXPayService().doWxRefund("6aaacbdbe85748fd985286c4c2148c04", "1", "1");

    }


    /**
     * 商户直连微信退款
     * @param orderNo 订单号
     * @param totalFee 订单总金额
     * @param refundFee 需退款金额
     * @return
     */
    public boolean doWxRefund( String orderNo, String totalFee, String refundFee){

        Map<String, String> data = new HashMap<>();
        data.put("out_trade_no", orderNo);
        data.put("total_fee", totalFee);
        data.put("refund_fee", refundFee);

        data.put("out_refund_no", orderNo);
        try {
            logger.info(data.toString());
            Map<String, String> result = wxPay.refund(data);
            logger.info(result.toString());
            if (result != null) {

                String return_code = result.get("return_code");

                logger.info(return_code);

                if (success.equals(return_code)) {
                    String result_code = result.get("result_code");
                    logger.info(result_code);
                    if (success.equals(result_code)) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
}
